﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.IO;

public partial class Admin_UserControl_QL_SanPham : System.Web.UI.UserControl
{
    string targetFolder;
    string targetFileName;
    UploadedFile file;
    DaiCuongMobileTableAdapters.tbProductsTableAdapter adtProduct = new DaiCuongMobileTableAdapters.tbProductsTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblWarning.Visible = false;
            Library.BindToDropDownList(ddlCategory);
        }
    }
    protected void btnSaveProduct_Click(object sender, EventArgs e)
    {
        try
        {
            string ProductName = txtProductName.Text.Trim();
            string ProductNameNoSign = txtProductNameNoSign.Text.Trim();
            int CategoryID =ddlCategory.IsEmpty? 1:Convert.ToInt32(ddlCategory.SelectedValue);
            int SupplierID =ddlSupplier.IsEmpty? 1: Convert.ToInt32(ddlSupplier.SelectedValue);
            decimal Price = txtPrice.Value.HasValue ? Convert.ToDecimal(txtPrice.Text) : 0;
            decimal SalePrice =txtPrice.Value.HasValue? Convert.ToDecimal(txtSalePrice.Text) : 0;
            int Quantity = txtQuantity.Value.HasValue ? Convert.ToInt32(txtQuantity.Text) : 0;
            DateTime PostDate = DateTime.Now;
            string Desciptions = txtDescriptions.Content;
            bool VisablePrice = ChBVisablePrice.Checked;
            string BigImageURL = UploadImage(RadUpload_BigImageURL);
            string SmallImageURL = UploadImage(RadUpload_SmallImageURL);
            if (adtProduct.Insert(ProductName, ProductNameNoSign, CategoryID, SupplierID, Price, SalePrice, Quantity, BigImageURL, SmallImageURL, PostDate, null, Desciptions, VisablePrice) > 0)
            {
                lblWarning.Text = "Đã thực hiện thành công!!";
                lblWarning.CssClass = "Success";
                lblWarning.Visible = true;
                gvProducts.Rebind();
            }
            else
            {
                lblWarning.Text = "Chưa thực hiện được.";
                lblWarning.CssClass = "Warning";
                lblWarning.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblWarning.Text = "Không thể thực hiện!!";
            lblWarning.CssClass = "Warning";
            lblWarning.Visible = true;
        }
       
    }
    protected void txtProductName_TextChanged(object sender, EventArgs e)
    {
        txtProductNameNoSign.Text = Library.ConvertToUnSign(txtProductName.Text.Trim());
    }

    protected string UploadImage(RadUpload fileUpload)
    {
        if (fileUpload.UploadedFiles.Count > 0)
        {
            int counter = 1;
            file = fileUpload.UploadedFiles[0];

            targetFolder = Server.MapPath(fileUpload.TargetFolder);

            targetFileName = Path.Combine(targetFolder,
                file.GetNameWithoutExtension() + file.GetExtension());

            while (System.IO.File.Exists(targetFileName))
            {
                targetFileName = Path.Combine(targetFolder,
                    file.GetNameWithoutExtension() + "_" + counter.ToString() + file.GetExtension());
                counter++;
            }
            /* Lưu ảnh lên server */
            file.SaveAs(targetFileName);
            /* tên file: anh.jpg */
            targetFileName = fileUpload.TargetFolder.ToString() + "/" + file.GetName().ToString();
            /* Lưu đường dẫn ảnh vào database */           
        }
        return targetFileName;
    }
}